<div class="config-container config-general">
<h3 class="section-title">Basic Settings</h3>

<%= form_for @pipeline, :as => :pipeline, :url => pipeline_update_path(:pipeline_name => @pipeline.name(), :current_tab => 'general'), :html => {:method => :put, :id => "pipeline_edit_form"} do |f| %>
    <%= md5_field %>
    <%= hidden_field_tag :pipeline_digest, @pipeline_digest %>
    <%= hidden_field_tag :pipeline_group_name, @pipeline_group_name %>
    <div class="fieldset">
        <div class="form_item">
            <div class="form_item_block required">
                <%= required_label(f, com.thoughtworks.go.config.PipelineConfig::NAME, 'Pipeline Name') -%>
                <%= f.text_field com.thoughtworks.go.config.PipelineConfig::NAME, :readonly => :readonly, :disabled=> :disabled -%>
                <%= error_message_on(@pipeline, com.thoughtworks.go.config.PipelineConfig::NAME, :css_class => "form_error") %>
            </div>

            <div class="form_item_block required">
                <%= required_label(f, com.thoughtworks.go.config.PipelineConfig::LABEL_TEMPLATE, 'Label Template') -%>
                <%= f.text_field com.thoughtworks.go.config.PipelineConfig::LABEL_TEMPLATE -%>
                <div class="contextual_help has_go_tip_right" title="Customize the label for this pipeline. <a class='' href='<%= docs_url '/configuration/admin_use_custom_pipeline_label.html' %>' target='_blank'>More...</a>"></div>
                <%= error_message_on(@pipeline, com.thoughtworks.go.config.PipelineConfig::LABEL_TEMPLATE, :css_class => "form_error") %>
            </div>

            <% if @pipeline.hasTemplate() -%>
                <%- first_stage_of_template = first_stage_of_template(@cruise_config, @pipeline.getTemplateName()) -%>
                <%= f.fields_for :approval, first_stage_of_template.getApproval do |approval| %>
                    <div class="form_item_block checkbox_row">
                        <div class="checkbox_label_wrapper">
                            <%= approval.check_box com.thoughtworks.go.config.Approval::TYPE, {:disabled => :disabled, :name => :not_to_be_submitted}, com.thoughtworks.go.config.Approval::SUCCESS, com.thoughtworks.go.config.Approval::MANUAL -%>
                            <%= approval.label com.thoughtworks.go.config.Approval::TYPE, 'Automatic pipeline scheduling' -%>
                            <span class="contextual_help has_go_tip_right" title="If unchecked, this pipeline will only schedule in response to a Manual/API/Timer trigger. Unchecking this box is the same as making the first stage manual."></span>
                        </div>
                        <span class="inline_instruction">Since this pipeline is based on a template, automatic/manual behaviour is determined by the template (first stage)</span>
                    </div>
                <% end -%>
            <% else -%>
                <%= f.fields_for :approval, @pipeline.get(0).getApproval do |approval| %>
                    <div class="form_item_block checkbox_row">
                        <div class="checkbox_label_wrapper">
                            <%= approval.check_box com.thoughtworks.go.config.Approval::TYPE, {}, com.thoughtworks.go.config.Approval::SUCCESS, com.thoughtworks.go.config.Approval::MANUAL -%>
                            <%= approval.label com.thoughtworks.go.config.Approval::TYPE, 'Automatic pipeline scheduling' -%>
                        </div>
                        <div class="contextual_help has_go_tip_right" title="If unchecked, this pipeline will only schedule in response to a Manual/API/Timer trigger. Unchecking this box is the same as making the first stage manual."></div>
                    </div>
                <% end -%>
            <% end -%>
        </div>
    </div>
    <h3>Timer Settings</h3>
    <div class="fieldset">
        <div class="form_item">

            <%= f.fields_for com.thoughtworks.go.config.PipelineConfig::TIMER_CONFIG, @pipeline.timer do |t| %>
                <div class="form_item_block timer_block">
                    <%= t.label com.thoughtworks.go.config.TimerConfig::TIMER_SPEC, 'Cron Timer Specification' -%>
                    <%= t.text_field com.thoughtworks.go.config.TimerConfig::TIMER_SPEC -%>
                    <div class="contextual_help has_go_tip_right" title="A cron-like schedule to build the pipeline. For example to run a pipeline once every night at 10 pm on weekdays, use '0 0 22 ? * MON-FRI'. <a href='<%= docs_url '/configuration/configuration_reference.html#timer' %>' target='_blank'>More...</a>"></div>
                    <%= error_message_on(@pipeline.getTimer(), com.thoughtworks.go.config.TimerConfig::TIMER_SPEC, :css_class => "form_error") if @pipeline.getTimer() %>
                </div>
                <div class="form_item_block checkbox_row">
                    <div class="checkbox_label_wrapper">
                        <%= t.check_box com.thoughtworks.go.config.TimerConfig::TIMER_ONLY_ON_CHANGES -%>
                        <%= t.label com.thoughtworks.go.config.TimerConfig::TIMER_ONLY_ON_CHANGES, 'Run only on new material', :id => "pipeline_timer_onlyOnChanges_label" -%>
                    </div>
                    <div class="contextual_help has_go_tip_right" title="Run only if the pipeline hasn't previously run with the latest material(s). This option is typically useful when automatic pipeline scheduling is turned off."></div>
                    <div class="inline_instruction">For this pipeline to schedule conditionally, please ensure at least one of its materials has polling enabled.</div>
                </div>

            <% end %>

        </div>
    </div>

    <h3><%= "Pipeline locking behavior" %></h3>

    <div class="form_item_block">
      <div class="checkbox_row radio_button_row">
        <%= f.radio_button com.thoughtworks.go.config.PipelineConfig::LOCK_BEHAVIOR, com.thoughtworks.go.config.PipelineConfig::LOCK_VALUE_UNLOCK_WHEN_FINISHED -%>
        <%= f.label "#{com.thoughtworks.go.config.PipelineConfig::LOCK_BEHAVIOR}_#{com.thoughtworks.go.config.PipelineConfig::LOCK_VALUE_UNLOCK_WHEN_FINISHED.downcase}", 'Run single instance of pipeline at a time' -%>
        <div class="contextual_help has_go_tip_right" title="Only a single instance of the pipeline will be run at a time and the pipeline will NOT be locked upon failure. The pipeline will only be locked to ensure a single instance, but will be unlocked if the pipeline finishes (irrespective of status) or reaches a manual stage."></div>
      </div>

      <div class="checkbox_row radio_button_row">
        <%= f.radio_button com.thoughtworks.go.config.PipelineConfig::LOCK_BEHAVIOR, com.thoughtworks.go.config.PipelineConfig::LOCK_VALUE_LOCK_ON_FAILURE -%>
        <%= f.label "#{com.thoughtworks.go.config.PipelineConfig::LOCK_BEHAVIOR}_#{com.thoughtworks.go.config.PipelineConfig::LOCK_VALUE_LOCK_ON_FAILURE.downcase}", 'Run single instance of pipeline and lock on failure' -%>
        <div class="contextual_help has_go_tip_right" title="Only a single instance of the pipeline will be run at a time and the pipeline will be locked upon failure. The pipeline can be unlocked manually and will be unlocked if it reaches the final stage, irrespective of the status of that stage. This is particularly useful in deployment scenarios."></div>
      </div>

      <div class="checkbox_row radio_button_row">
        <%= f.radio_button com.thoughtworks.go.config.PipelineConfig::LOCK_BEHAVIOR, com.thoughtworks.go.config.PipelineConfig::LOCK_VALUE_NONE -%>
        <%= f.label "#{com.thoughtworks.go.config.PipelineConfig::LOCK_BEHAVIOR}_#{com.thoughtworks.go.config.PipelineConfig::LOCK_VALUE_NONE.downcase}", 'Run multiple instances (default)' -%>
        <div class="contextual_help has_go_tip_right" title="This pipeline will not be locked and multiple instances of this pipeline will be allowed to run (default)."></div>
      </div>

      <%= error_message_on(@pipeline, com.thoughtworks.go.config.PipelineConfig::LOCK_BEHAVIOR, :css_class => "form_error") %>
    </div>

    <%= render :partial => 'shared/form_required_message.html', :locals => {:scope => {}} unless @is_config_repo_pipeline %>

    <%= render :partial => "admin/shared/form_submit", :locals => {:scope => {:reset_url => pipeline_edit_path(:pipeline_name => params[:pipeline_name], :current_tab => 'general')}} unless @is_config_repo_pipeline %>
    <%= render :partial => 'shared/convert_tool_tips.html', :locals => {:scope => {}} %>
<% end %>
</div>
<script type="text/javascript">
    var timeSpecTexbox = jQuery("#pipeline_timer_timerSpec");
    var onlyOnChangeCheckbox = jQuery("#pipeline_timer_onlyOnChanges");
    var onlyOnChangeLabel = jQuery("#pipeline_timer_onlyOnChanges_label");

    function enableCheckboxAndLabel() {
        onlyOnChangeCheckbox.removeAttr("disabled");
        onlyOnChangeLabel.removeClass("disabled");
    }

    function disableCheckboxAndLabel() {
        onlyOnChangeCheckbox.attr("disabled", "disabled");
        onlyOnChangeLabel.addClass("disabled");
    }

    Util.on_load(function() {
        if (timeSpecTexbox.val()) {
            enableCheckboxAndLabel();
        }
        else {
            disableCheckboxAndLabel();
        }
        timeSpecTexbox.on("keyup", function() {
            if (this.value == "") {
                disableCheckboxAndLabel();
            }
            else {
                enableCheckboxAndLabel();
            }
        });
    });
</script>


